import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:flutter/material.dart';

class ReimburseModel {
  static const quotation = 'Quotation';
  static const invoice = 'Invoice';
  static const transfer = 'Transfer';
  static const cashIn = 'Money-In';
  static const cashOut = 'Money-Out';
  static const purchaseOrder = 'Purchase Order';
  static const bill = 'Bill';
  static const chasOut = 'Money-Out';
  static const task = 'Task';
  static const creditNote = 'Credit Note';
  static const debitNote = 'Debit Note';
  static const expense = 'expense';
  static const leave = 'Leave';
  static const payslip = 'Payslip';
  static const gstF5 = 'gstF5';
}

class FormUtils {
  //第一个大写
  static String toUpperCaseFirst(String value) {
    if (value != null && value.length > 0) {
      return '${value[0].toUpperCase()}${value.substring(1, value.length)}';
    }
    return value;
  }

  //第一个小写
  static String toLowerCaseFirst(String value) {
    if (value != null && value.length > 0) {
      return '${value[0].toLowerCase()}${value.substring(1, value.length)}';
    }
    return value;
  }

  //获取当前报销单进度
  static String getStatus(String model, String status, num unpaid) {
    if (model == ReimburseModel.creditNote ||
        model == ReimburseModel.debitNote) {
      if (status == 'open') {
        return 'Credit Available';
      } else {
        return 'Fully Credited';
      }
    }
    List<String> statusList = getStatusList(model, status, unpaid);
    switch (status?.toLowerCase()) {
      case 'open':
      case 'pending':
      case 'unsubmitted':
        return statusList.first;
      case 'rejected':
      case 'approved':
      case 'collected':
      case 'later':
      case 'closed':
      case 'now':
      case 'paid':
      case 'withdrew':
        return statusList.last;
      case 'accepted':
      case 'overdue':
      case 'billed':
      case 'submitted':
        if (model == ReimburseModel.expense ||
            model == ReimburseModel.invoice ||
            model == ReimburseModel.bill) {
          return statusList.first;
        }
        return statusList[1];
    }
    return model;
  }

  //获取报销单状态进度
  static List<String> getStatusList(
      String model, String status, num unpaidCode) {
    String open = 'Open';
    String billed = 'Billed';
    String closed = 'Closed';
    String unpaid = 'Unpaid';
    String overdue = 'Overdue';
    String paid = 'Paid';
    String accepted = 'Accepted';
    String pending = 'Pending';
    String rejected = 'Rejected';
    String approve = 'Approved';
    String collected = 'Collected';
    String addToPayslip = 'Added To Payslip';

    String unSubmitted = 'Unsubmitted';
    String submitted = 'Submitted';
    String paidClaim = 'Paid/Claimed';
    String withdraw = 'Withdraw';

    switch (status?.toLowerCase()) {
      case 'pending':
      case 'open':
        if (model == ReimburseModel.purchaseOrder) {
          return [
            open,
            billed,
            closed,
          ];
        } else if (model == ReimburseModel.quotation) {
          return [
            open,
            accepted,
            closed,
          ];
        } else if (model == ReimburseModel.payslip) {
          return [unpaid, paid];
        } else if (model == ReimburseModel.gstF5) {
          return [
            unSubmitted,
            submitted,
            paidClaim,
          ];
        } else if (model == ReimburseModel.expense && unpaidCode == 0) {
          return [
            pending,
            paid,
          ];
        } else if (model == ReimburseModel.expense && unpaidCode == 1) {
          return [
            unpaid,
            paid,
          ];
        } else if (model == ReimburseModel.invoice ||
            model == ReimburseModel.bill) {
          return [
            unpaid,
            paid,
          ];
        } else if (model == ReimburseModel.leave) {
          return [
            pending,
            approve,
          ];
        }
        return [
          pending,
          paid,
        ];
      case 'billed':
        return [
          open,
          billed,
          closed,
        ];
      case 'now':
      case 'closed':
        if (model == ReimburseModel.purchaseOrder) {
          return [
            open,
            billed,
            closed,
          ];
        } else if (model == ReimburseModel.quotation) {
          return [
            open,
            accepted,
            closed,
          ];
        } else if (model == ReimburseModel.payslip) {
          return [unpaid, paid];
        } else if (model == ReimburseModel.gstF5) {
          return [
            unSubmitted,
            submitted,
            paidClaim,
          ];
        } else if (model == ReimburseModel.expense && unpaidCode == 0) {
          return [
            pending,
            paid,
          ];
        } else if (model == ReimburseModel.expense && unpaidCode == 1) {
          return [
            unpaid,
            paid,
          ];
        } else if (model == ReimburseModel.invoice ||
            model == ReimburseModel.bill) {
          return [
            unpaid,
            paid,
          ];
        }
        return [
          pending,
          paid,
        ];
      case 'rejected':
        return [
          pending,
          rejected,
        ];
      case 'approved':
        return [
          pending,
          approve,
        ];
      case 'overdue':
        if (model == ReimburseModel.quotation) {
          return [
            open,
            overdue,
            closed,
          ];
        } else if (model == ReimburseModel.expense ||
            model == ReimburseModel.invoice ||
            model == ReimburseModel.bill) {
          if (model == ReimburseModel.expense && unpaidCode == 0) {
            return [
              overdue,
              closed,
            ];
          }
          return [
            overdue,
            paid,
          ];
        } else {
          return [
            unpaid,
            overdue,
            paid,
          ];
        }
        break;
      case 'accepted':
        return [
          open,
          accepted,
          closed,
        ];
      case 'collected':
        return [
          unpaid,
          overdue,
          collected,
        ];
      case 'later':
        return [
          pending,
          addToPayslip,
        ];
      case 'unsubmitted':
      case 'submitted':
      case 'paid':
        return [
          unSubmitted,
          submitted,
          paidClaim,
        ];
      case 'withdrew':
        return [
          pending,
          withdraw,
        ];
    }
    return [];
  }

  // 获取进度的颜色
  static Color getStatusColor(String value) {
    switch (value?.toLowerCase()) {
      case 'closed':
      case 'now':
      case 'approved':
      case 'collected':
      case 'later':
      case 'paid':
        return aceGreenColor;
      case 'billed':
      case 'accepted':
      case 'submitted':
        return aceOrangeColor;
      case 'overdue':
        return aceRedColor;
    }
    return aceRedColor;
  }

  //转换为需要显示的name
  static String keyToName(String value) {
    List<String> formatList =
        value.split('_').map((e) => toUpperCaseFirst(e)).toList();
    return formatList.join(' ');
  }

  //还原为之前的key
  static String recoveryToKey(String value) {
    if (value == 'Delivery Order') return 'delivery';
    List<String> formatList =
        value.split(' ').map((e) => toLowerCaseFirst(e)).toList();
    return formatList.join('_');
  }

  static String getEmployeeName(EmployeeContactEntity employee) {
    if (employee?.name == null) return null;
    bool isMe = Config.userProfile.id == employee.userId;
    return '${employee.name}${isMe ? '(Me)' : ''}';
  }
}
